﻿using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Xml.Linq;
//
using PerceptiveMCAPI.Types.Internal;
//
namespace PerceptiveMCAPI.Types
{
   public class campaignAdviceOutput : Api_BaseOutput
   {
      public List<campaignAdviceResults> result { get; set; }
      public campaignAdviceParms inputParms { get; private set; }
      //
      public campaignAdviceOutput( campaignAdviceInput input )
         : base( input )
      {
         inputParms = input.parms;
         result = new List<campaignAdviceResults>();
      }
      // --------------------------------------------------- tostring
      public override string ToString()
      {
         return string.Format( "Results count: {0}", this.result.Count );
      }

      #region ================================================================== format setup

      // ---- serial
      internal void Format( string api_Request, string api_Response )
      {
         this.api_Request = api_Request;
         this.api_Response = api_Response;
         // ----------------------------------
         if ( this.api_OutputType == EnumValues.OutputType.JSON )
            outputJSON();
         else if ( this.api_OutputType == EnumValues.OutputType.XML )
            outputXML();
      }

      // ---- xmlrpc
      internal void Format( x_campaignAdvice[] rpcResult )
      {
         this.api_Request = EnumValues.NotSupplied;
         this.api_Response = EnumValues.NotSupplied;
         // ------------------------
         this.result = FormatResults( rpcResult );
      }

      #endregion

      #region ================================================================= Output

      // ---------------------------------------------------------- outputXML
      private void outputXML()
      {
         XElement xml = XElement.Parse( this.api_Response );
         // error check
         Api_Error error = apiSerial.GetXmlOutputError( xml );
         if ( error != null )
         {
            this.api_ErrorMessages.Add( error );
            return;
         }
         // -------------------------------------------- get values
         try
         {
            IEnumerable<XElement> entries = xml.Elements( "struct" );
            foreach ( var entry in entries )
            {
               var result = new campaignAdviceResults();
               result.msg = entry.Element( "msg" ).Value;
               result.type = entry.Element( "type" ).Value;
               this.result.Add( result );
            }
         }
         catch ( Exception ex )
         {
            this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }
      // ---------------------------------------------------------- outputJSON
      private void outputJSON()
      {
         JavaScriptSerializer jss = new JavaScriptSerializer();
         try
         {
            if ( this.api_Response.Contains( EnumValues.JSONErrorString ) )
            {
               Api_Error err = jss.Deserialize<Api_Error>( this.api_Response );
               this.api_ErrorMessages.Add( err );
            }
            else
            {
               x_campaignAdvice[] jsonResult = jss.Deserialize<x_campaignAdvice[]>( this.api_Response );
               this.result = FormatResults( jsonResult );
            }
         }
         catch ( Exception ex )
         {
            this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }

      // ----------------------------------------------------------------------- FormatResults
      private List<campaignAdviceResults> FormatResults( x_campaignAdvice[] x_results )
      {
         List<campaignAdviceResults> results = new List<campaignAdviceResults>();

         foreach ( x_campaignAdvice entry in x_results )
         {
            campaignAdviceResults result = new campaignAdviceResults { msg = entry.msg, type = entry.type };
            results.Add( result );
         }
         return results;
      }

      #endregion
   }
}
